package com.example.commonlib

import android.util.Log

object CLogUtil {

    private const val tag = "Leige"

    fun d(msg: String) {
        Log.d(tag, getMethodName() + " $msg")
    }

    fun e(msg: String) {
        Log.e(tag, getMethodName() + " $msg")
    }

    fun w(msg: String) {
        Log.w(tag, getMethodName() + " $msg")
    }

    private fun getMethodName(): String? {
        val traceElements = Thread.currentThread().stackTrace
        var index = 0;
        for (i in traceElements.indices) {
            val ele = traceElements[i]
            if (ele.className == CLogUtil.javaClass.name) {
                index = i
            }
        }

        if (index + 1 < traceElements.size) {
            val stackTraceElement = traceElements[index + 1]
            val className = stackTraceElement.className
            val substring = className.substring(stackTraceElement.className.lastIndexOf(".") + 1)
            return "$substring#${stackTraceElement.methodName}"
        }

        return ""
    }
}